//
//  MedicalRecordViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/3/4.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "MedicalRecordViewController.h"
#import <DOPDropDownMenu.h>
#import "DataManager.h"
#import "PatientData.h"
#import <Masonry.h>
#import "WriteMedicalViewController.h"
#import "Medical.h"
#import "MedicalViewController.h"

#define kShared [DataManager defaultManager]

@interface MedicalRecordViewController ()<DOPDropDownMenuDataSource,DOPDropDownMenuDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) DOPDropDownMenu *downMenu;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) PatientData *patient;
@property (nonatomic,assign) int flag;
@property (nonatomic,assign) BOOL OK;
@end

@implementation MedicalRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if([kShared.userType isEqualToString:@"医生"])
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"写病历" style:UIBarButtonItemStyleDone target:self action:@selector(didClickRightBtn)];
    }
    //将就诊人绑定到下拉列表上
    [kShared getDataArrayAndBlock:^{
        self.OK = YES;
        [self.downMenu reloadData];
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"电子病历";
    
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.OK = NO;
    self.flag = 0;
    [self addViews];
    

}


//页面布局
- (void)addViews
{
    // 添加下拉菜单
    self.downMenu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:44];
    self.downMenu.delegate = self;
    self.downMenu.dataSource = self;
    [self.view addSubview:self.downMenu];
    [self.downMenu selectDefalutIndexPath];
    //添加tableView
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downMenu).offset(44);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

//点击写病历触发的事件
- (void)didClickRightBtn
{
    WriteMedicalViewController *writeMedicalVC = [[WriteMedicalViewController alloc] init];
    writeMedicalVC.dataArray = kShared.dataArray;
    [self.navigationController pushViewController:writeMedicalVC animated:YES];
}

/**
 *  返回 menu 第column列有多少行
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    return kShared.dataArray.count;
}

/**
 *  返回 menu 第column列 每行title
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if(kShared.dataArray.count == 0)
    {
        return  nil;
    }
    PatientData *patient = kShared.dataArray[indexPath.row];
    
    if(self.OK)
    {
        [kShared getMedicalArrayWithPatient:patient AndBlock:^{
            [self.tableView reloadData];
        }];
        self.OK = NO;
        self.flag = 1;
    }
    return patient.name;
}



/**
 *  点击代理，点击了第column 第row 或者item项，如果 item >=0
 */
- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //如果已经刷新了一次视图了就不需要再刷新了，否则有可能数据出错
    if(self.flag == 1)
    {
        if(self.OK == NO)
        {
            PatientData *patient = kShared.dataArray[indexPath.row];
            [kShared getMedicalArrayWithPatient:patient AndBlock:^{
                [self.tableView reloadData];
            }];
        }
        self.flag = 2;
    }
    else if(self.flag != 0)
    {
        PatientData *patient = kShared.dataArray[indexPath.row];
        [kShared getMedicalArrayWithPatient:patient AndBlock:^{
            [self.tableView reloadData];
        }];
    }
}

#pragma mark tableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return kShared.medicalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
    Medical *medical = kShared.medicalArray[indexPath.row];
    cell.textLabel.text = medical.patient.name;
    NSString *timeStr = [medical.writeTime substringToIndex:11];
    cell.detailTextLabel.text = timeStr;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(kShared.medicalArray.count == 0)
    {
        [self showLabelWithString:@"点的太快了，让我缓缓吧" AndBlock:nil];
    }
    else
    {
        MedicalViewController *medicalVC = [[MedicalViewController alloc] init];
        medicalVC.medical = kShared.medicalArray[indexPath.row];
        [self.navigationController pushViewController:medicalVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
